#!/bin/sh
#------------------------------------------------
#Wifi Connect script
VERSION=v1.2
#Created by: SymPak
#------------------------------------------------
usage () {
	echo ""
	echo "Wifi Connect $VERSION"
	echo "Created by: SymPak"
	echo ""
	echo "USAGE: ./connect.sh [-BRWkr] [-i <wlan0|wlan1>] [-s SSID] [-p Wifi Password] [-t Interface] [-b SSID] [-w SSID]..."
	echo ""
	echo "OPTIONS:"
	echo "-B 	Backup /etc/config/wireless"
	echo "-R 	Restore /etc/config/wireless from backup"
	echo "-k	Start Karma"
	echo "-r 	Random MAC"
	echo "-i	Interface"
	echo "-s	SSID"
	echo "-p	Wifi Password"
	echo "-t 	To interface for ICS. From interface is specified with -i"
	echo "-w	Whitelists AP by SSID. Scans with WLAN0 if no interface is specified. Unencrypted netowrks only"
	echo "-W	Checks for and connects to networks specified in the whitelist. Unencrypted netowrks only"
	echo "-b	Blacklists AP by SSID. Scans with WLAN0 if no interface is specified. Unencrypted netowrks only"
	exit
}
#------------------------------------------------
#CONSTANTS
#------------------------------------------------
PWD="$(pwd)"
WIFI_LIST=/tmp/wifi_list.tmp
OPEN_WIFI=/tmp/open_wifi.tmp
CONFIG=/etc/config/wireless
BACKUP_CONFIG=$PWD/wireless.bak
BLACKLIST=$PWD/blacklist.txt
WHITELIST=$PWD/whitelist.txt
RUN_LOG=/tmp/wificonnectrun.log
ERROR_LOG=/tmp/errorlog.tmp
NETWORK_ADDRESS=172.16.42.0
#------------------------------------------------
#Script start handling
#------------------------------------------------
if [[ -z "$1" ]]; then 
	usage
fi
while getopts ":BRDkrWi:s:p:m:w:b:t:" ARGS; do
	case $ARGS in
		B) BACKUP=1;;
		R) RESTORE=1;;
		D) DEBUG=1;;
		k) START_KARMA=1;;
		r) RANDOM_MAC=1;;
		i) INTERFACE="$OPTARG"; if [[ -n "$INTERFACE" ]]; then WIFI_INT="$(echo $INTERFACE | tr [A-Z] [a-z])"; fi;;
		s) ESSID="$OPTARG";;		
		p) PASSWORD="$OPTARG";;
		W) CHECK_WHITELIST=1;;
		w) WHITE_AP="$OPTARG";;
		b) BLACK_AP="$OPTARG";;
		#m) AP_MAC="$OPTARG";;
		t) ICS_TO="$OPTARG"
		VALID_INT="$(ifconfig | awk {'print $1'} | grep -i $ICS_TO)"
		if [[ "$ICS_TO" == "$VALID_INT" ]]; then
			echo "Internet connection sharing will be set up between $WIFI_INT and $ICS_TO once connected"
		else
			echo "Invalid interface $ICS_TO"
			unset ICS_TO
		fi;;
		help) usage;;
		\?) usage;;
		:) echo "-$OPTARG requires an argument"; usage;;
	esac
done
#-------------------------------------------------
#Checks to see if the interface is set up in
#/etc/config/wireless and checks startup arguments
#-------------------------------------------------
start_check () {
	if [[ "$BACKUP" == "1" ]]; then backup_config; 
		if [[ -z "$WIFI_INT" ]]; then quit; fi
	fi
	if [[ "$RESTORE" == "1" ]]; then restore_config; 
		if [[ -z "$WIFI_INT" ]]; then quit; fi
	fi
	if [[ -n "$BLACK_AP" ]] || [[ -n "$WHITE_AP" ]]; then 
		if [[ -z "$WIFI_INT" ]]; then
			WIFI_INT=wlan0
			echo "No interface specified. Using wlan0"
		fi
	fi
	VALID_INT="$(ifconfig | awk {'print $1'} | grep -i wlan | grep -i "$WIFI_INT")"
	if [[ "$WIFI_INT" == "$VALID_INT" ]]; then
		echo "Starting on $WIFI_INT"
		INT="$(echo "$WIFI_INT" | sed 's/wlan//')"
	else
		echo "Invalid interface $WIFI_INT"
		quit
	fi
	if [[ -n "$BLACK_AP" ]]; then set_blacklist; fi
	if [[ -n "$WHITE_AP" ]]; then set_whitelist; fi
	wps_led 3
	uci get wireless.@wifi-device[$INT] 2> $ERROR_LOG >> $RUN_LOG
	i=1
	while [[ -s $ERROR_LOG ]] && [[ $i -le 3 ]]
	do
		echo "/etc/config/wireless not set up for $WIFI_INT."
		echo "Setting up /etc/config/wireless. $i of 3"
		i=$(( $i + 1 ))
		if [[ $i -ge 4 ]]; then quit; fi
		wifi detect $WIFI_INT >> /etc/config/wireless
		uci get wireless.@wifi-device[$INT] 2> $ERROR_LOG >> $RUN_LOG
	done
	karma stop
}
#------------------------------------------------
#Backs up /etc/config/wireless
#------------------------------------------------
backup_config () {
	if [[ -s $BACKUP_CONFIG ]]; then
		echo "$BACKUP_CONFIG already exists. Overwriting"
		rm -f $BACKUP_CONFIG
		cp $CONFIG $BACKUP_CONFIG
		if [[ -s $BACKUP_CONFIG ]]; then
			echo "Wireless configuration file backed up"
		fi
	else
		cp $CONFIG $BACKUP_CONFIG
		echo "Wireless configuration file backed up"
	fi
}
#------------------------------------------------
#Restores /etc/config/wireless
#------------------------------------------------
restore_config () {
	if [[ -s $BACKUP_CONFIG ]]; then
		mv $BACKUP_CONFIG $CONFIG
		echo "Wireless configuration file restored from backup"
	else
		echo "$BACKUP_CONFIG not found"
	fi
}
#------------------------------------------------------
#Scans for wifi and saves results to /tmp/wifi_list.tmp
#------------------------------------------------------
wifi_scan () {
	if [[ ! -f "$WIFI_LIST" ]]; then
		echo "Scanning for wifi"
		iwlist "$WIFI_INT" scan 2> $ERROR_LOG | egrep -i "Address:|encryption key|essid|channel:|quality|WPA2|WPA|Pairwise Ciphers" > "$WIFI_LIST"
		i=1
		while [[ -s $ERROR_LOG ]] && [[ "$i" -le "3" ]]
		do
			echo "Scan not completed. Retrying. "$i" of 3"
			ifconfig "$WIFI_INT" down 2>> $RUN_LOG
			sleep 1
			ifconfig "$WIFI_INT" up 2>> $RUN_LOG
			sleep 3
			iwlist "$WIFI_INT" scan 2> $ERROR_LOG | egrep -i "Address:|encryption key|essid|channel:|quality|WPA2|WPA|Pairwise Ciphers" > "$WIFI_LIST"
			i=$(( $i + 1 ))
			if [[ "$i" -ge "4" ]]; then echo "Cannot scan for wifi. Interface down or unavailable."; quit; fi
		done
	fi
}
#------------------------------------------------
#Searches for wifi and gets the specifics
#------------------------------------------------
get_wifi () {
	wifi_scan
	if [[ -n "$ESSID" ]]; then
		AP="$(cat "$WIFI_LIST" | grep -i "$ESSID" | head -n1 | sed 's/ESSID://;s/\"//g;s/^[ \t]*//;/^$/d')"
		if [[ -z "$AP" ]];then
			echo "$ESSID not found"
			unset ESSID
		fi
	fi
	if [[ -z "$ESSID" ]]; then
		get_unencrypted_wifi
	else
		get_encrypted_wifi
	fi
}
#------------------------------------------------
#Scans for an unencrypted wifi access point and
#sets the ssid and channel
#------------------------------------------------
get_unencrypted_wifi () {
	cat $WIFI_LIST | egrep -i "Address:|encryption key|essid|channel:|quality" | grep -iB3 -A1 'encryption key:off' > $OPEN_WIFI
	if [[ "$CHECK_WHITELIST" == "1" ]]; then get_whitelist; fi
	if [[ -n "$ESSID" ]]; then
		WIFI_SSID="$ESSID"
	else
		get_blacklist
		WIFI_SSID="$(cat $OPEN_WIFI | grep -iA1 'encryption key:off' | grep -i 'ESSID:' | sed 's/ESSID://;s/\"//g;s/^[ \t]*//;/^$/d' | head -n 1)" 
	fi
	if [[ -z "$WIFI_SSID" ]]; then 
		echo "No Wifi available. Aborting."
		if [[ "$DEBUG" != "1" ]]; then
			wps_led 2
		fi
		quit
	fi
	WIFI_CHANNEL="$(cat "$OPEN_WIFI" | egrep -i 'Quality|essid|Channel:' | grep -B3 "$WIFI_SSID" | grep 'Channel:' | sed 's/Channel://;s/^[ \t]*//' | head -n 1)"
	QUALITY="$(cat "$OPEN_WIFI" | egrep -i 'Quality|essid|Channel:' | grep -B3 "$WIFI_SSID" | grep 'Quality' | awk {'print $1'} | sed 's/Quality=//;s/\/70//' | head -n1)"
	if [[ -n "$QUALITY" ]]; then
		WIFI_QUALITY="$(expr "$QUALITY" \* 100 / 70)%"
	fi
}
#------------------------------------------------
#Checks for an encrypted wifi access point and
#sets the ssid, channel and encryption mode
#------------------------------------------------
get_encrypted_wifi () {
	ENCRYPTION_OFF="$(cat "$WIFI_LIST" | grep -iB1 "$ESSID" | grep -i 'encryption key:off')"
	if [[ -z "$ENCRYPTION_OFF" ]]; then
		WPA="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep 'WPA Version 1')"
		WPA2="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep 'WPA2 Version 1')"
		if [[ -n "$WPA" ]] && [[ -n "$WPA2" ]]; then
			PW="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA2 Version 1' | tail -n1 | awk {'print $3'})"
			if [[ "$PW" == "(1)" ]]; then
				WPA2_CIPHER="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA2 Version 1' | tail -n1 | awk {'print $5'} | tr [A-Z] [a-z])"
			elif [[ "$PW" == "(2)" ]]; then
				WPA2_CIPHER="tkip+ccmp"
			fi
			ENCRYPTION='mixed-psk+'$WPA2_CIPHER
		elif [[ -n "$WPA2" ]]; then
			PW="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA2 Version 1' | tail -n1 | awk {'print $3'})"
			if [[ "$PW" == "(1)" ]]; then
				WPA2_CIPHER="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA2 Version 1' | tail -n1 | awk {'print $5'} | tr [A-Z] [a-z])"
			elif [[ "$PW" == "(2)" ]]; then
				WPA2_CIPHER="tkip+ccmp"
			fi
			ENCRYPTION='psk2+'$WPA2_CIPHER
		elif [[ -n "$WPA" ]]; then
			PW="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA Version 1' | tail -n1 | awk {'print $3'})"
			if [[ "$PW" == "(1)" ]]; then
				WPA_CIPHER="$(cat "$WIFI_LIST" | grep -iA7 "$ESSID" | grep -A1 'WPA Version 1' | tail -n1 | awk {'print $5'} | tr [A-Z] [a-z])"
			elif [[ "$PW" == "(2)" ]]; then
				WPA_CIPHER="tkip+ccmp"
			fi
			ENCRYPTION='psk+'$WPA_CIPHER
		else
			ENCRYPTION='wep'
		fi
	fi
	if [[ -n "$ENCRYPTION" ]] && [[ -z "$PASSWORD" ]]; then
		echo "You must specify a passkey for protected wifi"
		unset ESSID
		get_wifi
	else
		WIFI_KEY="$PASSWORD"
		WIFI_SSID="$(cat "$WIFI_LIST" | grep -i "$ESSID" | head -n1 | sed 's/ESSID://;s/\"//g;s/^[ \t]*//;/^$/d')"
		WIFI_ENCRYPTION="$ENCRYPTION"
		WIFI_CHANNEL="$(cat "$WIFI_LIST" | egrep -i 'Quality|essid|Channel:' | grep -B2 "$WIFI_SSID" | grep Channel: | sed 's/Channel://;s/^[ \t]*//' | head -n 1)"
		QUALITY="$(cat "$WIFI_LIST" | egrep -i 'Quality|essid|Channel:' | grep -B3 "$WIFI_SSID" | grep 'Quality' | awk {'print $1'} | sed 's/Quality=//;s/\/70//' | head -n1)"
		if [[ -n "$QUALITY" ]]; then
			WIFI_QUALITY="$(expr "$QUALITY" \* 100 / 70)%"
		fi
	fi
}
#------------------------------------------------
#Checks that the ssid is not null and configures
#the interface in /etc/config/wireless
#------------------------------------------------
set_wifi () {
	echo none > /sys/class/leds/alfa:blue:wps/trigger
	sleep 1
	if [[ -z "$WIFI_SSID" ]]; then 
		echo "No Wifi available. Aborting."
		if [[ "$DEBUG" != "1" ]]; then
			wps_led 2
		fi
		quit
	else
		echo "Connecting to $WIFI_SSID on channel $WIFI_CHANNEL signal quality $WIFI_QUALITY"
		uci set wireless.@wifi-iface[$INT].ssid="$WIFI_SSID"
		uci set wireless.@wifi-iface[$INT].encryption="$WIFI_ENCRYPTION"
		uci set wireless.@wifi-iface[$INT].key="$WIFI_KEY"
		uci set wireless.@wifi-device[$INT].channel="$WIFI_CHANNEL"
		uci set wireless.@wifi-iface[$INT].network=wan
		uci set wireless.@wifi-iface[$INT].mode=sta
		uci set wireless.@wifi-iface[$INT].bssid="$AP_MAC"
		uci commit wireless
		wifi >> $RUN_LOG 2>&1
		if [[ "$RANDOM_MAC" == "1" ]]; then run_macchanger; fi
	fi
}
#------------------------------------------------
#Scans for SSID and places BSSID in file
#------------------------------------------------
set_whitelist () {
	wifi_scan
	BSSID="$(cat "$WIFI_LIST" | egrep -i 'Address:|essid' | grep -iB1 "$WHITE_AP" | head -n1 | awk {'print $5'})"
	if [[ -z "$BSSID" ]]; then
		echo "Cannot find $WHITE_AP"
	else
		if [[ -e "$WHITELIST" ]]; then
			LIST="$(cat $WHITELIST | grep -i "$BSSID")"
			if [[ -z "$LIST" ]]; then
				echo "$BSSID" >> $WHITELIST
				echo "$WHITE_AP added to whitelist"
			else
				echo "Access point already whitelisted"
			fi
		else
			touch $WHITELIST
			LIST="$(cat $WHITELIST | grep -i "$BSSID")"
			if [[ -z "$LIST" ]]; then
				echo "$BSSID" >> $WHITELIST
				echo "$WHITE_AP added to whitelist"
			else
				echo "Access point already whitelisted"
			fi
		fi
	fi
	quit
}
#------------------------------------------------
#Checks /tmp/wifi_list.tmp for whitelisted APs
#------------------------------------------------
get_whitelist () {
	if [[ -s "$WHITELIST" ]]; then
		echo "Checking for whitelisted APs"
		WHITELENGTH="$(cat "$WHITELIST" | wc -l)"
		i=1
		while [[ "$i" -le "$WHITELENGTH" ]]
		do
			WHITELISTAP="$(cat "$WHITELIST" | awk {'print $1'} | head -n$i | tail -n1)"
			APFOUND="$(cat "$OPEN_WIFI" | grep -i "$WHITELISTAP")"
			if [[ -n "$APFOUND" ]]; then
				ESSID="$(cat "$OPEN_WIFI" | egrep -i 'Address:|ESSID:' | grep -iA1 "$WHITELISTAP" | grep 'ESSID:' | head -n1 | sed 's/ESSID://;s/\"//g;s/^[ \t]*//;/^$/d')"
				echo "Found $ESSID in whitelist."
				break
			fi
			i=$(( $i + 1 ))
		done
		if [[ -z "$APFOUND" ]]; then
			echo "No whitelisted APs in range."
			echo "Checking for available wifi"
		fi
	fi
}
#------------------------------------------------
#Finds the BSSID of an AP and puts it in 
#blacklist.txt
#------------------------------------------------
set_blacklist () {
	wifi_scan
	BSSID="$(cat "$WIFI_LIST" | egrep -i 'Address:|essid' | grep -iB1 "$BLACK_AP" | head -n1 | awk {'print $5'})"
	if [[ -z "$BSSID" ]]; then
		echo "Cannot find $BLACK_AP"
	else
		if [[ -e "$BLACKLIST" ]]; then
			if [[ ! -s "$BLACKLIST" ]]; then
				echo "$BSSID" >> $BLACKLIST
				echo "$BLACK_AP added to blacklist"
			else
				echo "$BLACK_AP already blacklisted"
			fi
		else
			touch $BLACKLIST
			if [[ ! -s "$BLACKLIST" ]]; then
				echo "$BSSID" >> $BLACKLIST
				echo "$BLACK_AP added to blacklist"
			else
				echo "$BLACK_AP already blacklisted"
			fi
		fi
	fi
	quit
}
#------------------------------------------------
#Blacklists local interfaces so the pineapple
#doesn't connect to itself. Also checks for and
#applies user configurable blacklists from
#blacklist.txt
#------------------------------------------------
get_blacklist () {
	TEMPBLACKLIST=/tmp/blacklist.tmp
	TEMPWIFILIST=/tmp/new_wifi_list.tmp
	LIST="$(cat $BLACKLIST 2> /dev/null)"
	ifconfig wlan0 | grep -i 'hwaddr' | awk {'print $5'} > $TEMPBLACKLIST
	ifconfig wlan1 | grep -i 'hwaddr' | awk {'print $5'} >> $TEMPBLACKLIST
	if [[ -n "$LIST" ]]; then
		echo "Applying user blacklist"
		i=1
		BLACKLENGTH="$(cat $BLACKLIST | wc -l)"
		while [ $i -le $BLACKLENGTH ]
		do
			cat "$BLACKLIST" | head -n"$i" | tail -n1 >> $TEMPBLACKLIST
			i=$(( $i + 1 ))
		done
	fi
	i=1
	BLACKLENGTH="$(cat $TEMPBLACKLIST | wc -l)"
	while [[ $i -le $BLACKLENGTH ]]
	do
		BLKLST="$(cat $TEMPBLACKLIST | head -n$i | tail -n1)"
		if [[ -z "$BLKLST" ]]; then
			echo "Blacklist empty"
		elif [[ -n "cat $OPEN_WIFI | grep -i "$BLKLST" | sed 's/^[ \t]*//'" ]]; then
			cat $OPEN_WIFI | sed -e "/${BLKLST}/{p;N;N;N;N;d}" | sed "/${BLKLST}/d" > $TEMPWIFILIST
			rm -f $OPEN_WIFI 
			mv $TEMPWIFILIST $OPEN_WIFI
		fi
		i=$(( $i + 1 ))
	done
}
#------------------------------------------------
#Gets an IP address
#------------------------------------------------
get_ip () {
	RELEASE="$(ps -ax | grep 'udhcpc' | grep "$WIFI_INT" | awk {'print $1'})"
	if [[ -n "$RELEASE" ]]; then
		kill "$RELEASE"
	fi
	echo "Requesting IP Address";
	udhcpc -nT 10 -i "$WIFI_INT" 2>> $RUN_LOG > /tmp/udhcpc.log
	NOLEASE="$(cat '/tmp/udhcpc.log' | grep -i 'No lease')"
	LEASE="$(cat '/tmp/udhcpc.log' | grep -i 'obtained')"
	if [[ -n "$NOLEASE" ]]; then
		echo "No lease, failing."
		if [[ "$DEBUG" != "1" ]]; then
			wps_led 1
		fi
		IS_ASSOCIATED="$(iwconfig "$WIFI_INT" | grep 'Access Point:' | awk {'print $4'})"
		if [[ -n "$IS_ASSOCIATED" ]] && [[ "$IS_ASSOCIATED" == "Not-Associated" ]]; then
			echo "Could not connect to $WIFI_SSID"
		fi
	elif [[ -n "$LEASE" ]]; then
		wps_led 3
		echo "Connected"
		setup_ics
		if [[ "$WIFI_INT" == "wlan1" ]]; then
			if [[ "$START_KARMA" == "1" ]]; then karma start; fi
	    fi
	fi
}
#------------------------------------------------
#Internet connection sharing (ICS)
#------------------------------------------------
setup_ics (){
	if [[ "$WIFI_INT" == "wlan0" ]]; then
		ICS_FROM="$WIFI_INT"
		if [[ -z "$ICS_TO" ]]; then ICS_TO=wlan1; fi
	elif [[ "$WIFI_INT" == "wlan1" ]]; then
		ICS_FROM="$WIFI_INT"
		if [[ -z "$ICS_TO" ]]; then ICS_TO=wlan0; fi
	fi
	INT_IS_VALID="$(ifconfig | awk {'print $1'} | grep -i "$ICS_TO")"
	if [[ -n "$INT_IS_VALID" ]]; then
		echo "Configuring ICS"
		#ICS commands borrowed from Whistle Master's networkmanager infusion
		echo 1 > /proc/sys/net/ipv4/ip_forward
		iptables -A FORWARD -i "$ICS_FROM" -o "$ICS_TO" -s "$NETWORK_ADDRESS" -m state --state NEW -j ACCEPT
		iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
		iptables -t nat -A POSTROUTING -o "$ICS_FROM" -j MASQUERADE
	else
		echo "Cannot configure ICS. $ICS_TO is invalid"
		echo "$ICS_TO may be down or disconnected"
		echo "Please specify interface with -t"
	fi
}
#------------------------------------------------
#Macchanger changes MAC of wlan0 and wlan1 to a
#random vendor MAC
#------------------------------------------------
run_macchanger (){
	i=0
	while [[ $i -le 1 ]]
	do
		echo "Changing MAC on wlan$i"
		ifconfig wlan$i down
		macchanger -A wlan$i
		ifconfig wlan$i up
		i=$(( $i + 1 ))
		sleep 1
	done
}
#------------------------------------------------
#Starts and stops karma
#------------------------------------------------
karma (){
	WLAN0_MODE="$(uci get wireless.@wifi-iface[0].mode | tr [A-Z] [a-z])"
	if [[ "$1" == "start" ]] && [[ "$WLAN0_MODE" == "ap" ]]; then
		echo "Starting karma"
		hostapd_cli -p /var/run/hostapd-phy0 karma_enable >> $RUN_LOG 2> $ERROR_LOG
		if [[ -s $ERROR_LOG ]]; then echo "Karma error. Check that wlan0 is up and in AP mode."; fi
	elif [[ "$1" == "stop" ]] && [[ "$WLAN0_MODE" == "ap" ]]; then
		echo "Stopping karma"
		hostapd_cli -p /var/run/hostapd-phy0 karma_disable >> $RUN_LOG 2> $ERROR_LOG
		if [[ -s $ERROR_LOG ]]; then echo "Karma error. Check that wlan0 is up and in AP mode."; fi
	fi
}
#--------------------------------------------------
#Displays status of the script using wps button led
#--------------------------------------------------
#LED STATUS CODES
#Solid = Connected and obtained IP Address (CODE 3)
#1 Flash = Could not obtain IP address (CODE 1)
#2 Flashes = No wifi available (CODE 2)
#------------------------------------------------
wps_led () {
	CODE=$1 
	if [[ "$CODE" == "3" ]]; then
	echo default-on > /sys/class/leds/alfa:blue:wps/trigger
	elif [[ "$CODE" -le "2" ]]; then
		i=1
		while [[ $i -le 2 ]]
		do
			i=$(( $i + 1 ))
			x=1
			while [[ $x -le $CODE ]]
			do
				x=$(( $x + 1 ))
				echo default-on > /sys/class/leds/alfa:blue:wps/trigger
				sleep 1
				echo none > /sys/class/leds/alfa:blue:wps/trigger
				sleep 1
			done
			sleep 2
		done
	fi
}
#------------------------------------------------
#Garbage cleanup and exiting the script
#------------------------------------------------
quit () {
	if [[ "$DEBUG" != "1" ]]; then
		rm -f $ERROR_LOG $WIFI_LIST $OPEN_WIFI $RUN_LOG $TEMPBLACKLIST /tmp/udhcpc.log /tmp/wifi_list.debug
	else
		mv $WIFI_LIST /tmp/wifi_list.debug
	fi
	echo "Done."
	exit
}
#------------------------------------------------
#Function calls
#------------------------------------------------
start_check
get_wifi
set_wifi
get_ip
quit